/**
 * Copyright (c) 2008 RiseSoft Co.,Ltd  
 * $Header$
 */
package net.risesoft.risebi.collection.web.admin;

import java.io.File;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.apache.commons.beanutils.LazyDynaBean;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import net.risesoft.risebi.collection.model.Indication;
import net.risesoft.risebi.collection.model.IndicationType;
import net.risesoft.risebi.collection.service.DbMetaDataManager;
import net.risesoft.risebi.collection.service.IndicationManager;
import net.risesoft.risebi.collection.web.BaseAction;
import net.risesoft.risebi.commons.util.GUID;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * indication action
 * 本类提供指标管理Action
 * @author <a href="mailto:gf7@163.com">关锋</a>
 * @version $Revision$
 * 
 */
@Namespace("/admin/IndicationAction")
public class IndicationAction extends BaseAction {
	private static final long serialVersionUID = 1631989578456103858L;

	private Indication indication;	
	private IndicationType indicationType;
	
	private IndicationManager indicationManager;
	private DbMetaDataManager dbMetaDataManager;
	
	private String indicationTypeGuid;	
	private String crud;
	private String field; //Ext.ux.plugins.RemoteValidator
	private String value; //Ext.ux.plugins.RemoteValidator

	@Action(value="prepareIndiTree",
			results={@Result(name="success", location="/admin/indication/indiTree.jsp")}
	)
	public String prepareIndiTree() throws Exception{
		return SUCCESS;
	}

	@Action(value="getIndicationTypeTreeNodes")
	public String getIndicationTypeTreeNodes() throws Exception {
		try{
			JSONArray jsonArray = new JSONArray();			
			if("indicationTreeRootNode".equals(this.node)){
				List<IndicationType> list =indicationManager.getAllIndicationTypeOrderByTabIndex();				
				for(IndicationType t : list){
					JSONObject jsonObj = new JSONObject();
					jsonObj.element("id", t.getGuid());
					jsonObj.element("text", "("+t.getCode()+") "+t.getName());
					jsonObj.element("source", "indicationType");
					jsonObj.element("indicationTypeGuid", t.getGuid());
					jsonObj.element("singleClickExpand", true);
					jsonObj.element("qtipCfg", "{title: '单击鼠标右键，出现上下文菜单', trackMouse: true}");
					
					boolean isleaf = true;
					int count = indicationManager.getCount("select count(*) from Indication t where t.indicationType.guid = '" + t.getGuid() + "' and t.parentIndication.guid is null");
					if(count>0){
						isleaf = false;
					}
					jsonObj.element("leaf", isleaf);
					
					jsonArray.add(jsonObj);
				}
			}else{
				List<Indication> list;
				if("indicationType".equals(this.source)){
					list = indicationManager.getIndicationByTypeOrderByTabIndex(this.node);
				}else{
					list = indicationManager.getIndicationOrderByTabIndex(this.node);
				}
				
				for(Indication t : list){
					JSONObject jsonObj = new JSONObject();
					jsonObj.element("id", t.getGuid());
					jsonObj.element("text", "("+t.getCode()+") "+t.getFullName());
					jsonObj.element("source", "indication");
					jsonObj.element("indicationTypeGuid", t.getIndicationType().getGuid());
					jsonObj.element("singleClickExpand", true);
					jsonObj.element("qtipCfg", "{title: '单击鼠标右键，出现上下文菜单', trackMouse: true}");
					
					boolean isleaf = true;
					int count = indicationManager.getCount("select count(*) from Indication t where t.parentIndication.guid = '" + t.getGuid() + "'");
					if(count>0){
						isleaf = false;
					}
					jsonObj.element("leaf", isleaf);
					
					jsonArray.add(jsonObj);
				}
			}

			this.json = jsonArray.toString();			
		}catch(Exception e){
			this.json = "[]";
			e.printStackTrace();
		} 
		
		return AJAX;
	}
	
	@Action(value="prepareIndiTypeEdit",
			results={@Result(name="success", location="/admin/indication/indiTypeEdit.jsp")}
	)
	public String prepareIndiTypeEdit()throws Exception {
		if(indicationType.getGuid()==null || indicationType.getGuid().trim().length()==0){
			indicationType.setGuid(new GUID().toString(32));
			indicationType.setTabIndex(100d);
		}else{
			indicationType = (IndicationType)indicationManager.getObject(IndicationType.class, indicationType.getGuid());
		}
		
		return SUCCESS;
	}
	
	@Action(value="updateIndiType")
	public String updateIndiType() throws Exception {
		JSONObject jsonObj = new JSONObject();
		try{
			indicationManager.saveObject(indicationType);
			
			jsonObj.element("success", true);
			jsonObj.element("reason", "保存成功!");
			
			jsonObj.element("guid", indicationType.getGuid());
			jsonObj.element("code", indicationType.getCode());
			jsonObj.element("name", indicationType.getName());
			
			this.json = jsonObj.toString();
		}catch(Exception e){
			jsonObj.element("success", false);
			jsonObj.element("reason", e.getMessage());
			this.json = jsonObj.toString();
		}
		
		return AJAX;
	}
	
	@Action(value="removeIndiType")
	public String removeIndiType() throws Exception {
		JSONObject jsonObj = new JSONObject();
		try{
			indicationManager.removeObject(IndicationType.class, node);
			
			jsonObj.element("success", true);
			jsonObj.element("reason", "删除成功!");
			this.json = jsonObj.toString();
		}catch(Exception e){
			jsonObj.element("success", false);
			jsonObj.element("reason", e.getMessage());
			this.json = jsonObj.toString();
		}		
		
		return AJAX;
	}
	
	@Action(value="validateIndiTypeCode")
	public String validateIndiTypeCode() throws Exception {
		LazyDynaBean ldb = new  LazyDynaBean();		
		String code = this.value.trim();
		if("c".equals(this.crud)){
			Integer count = indicationManager.getCount("select count(*) from IndicationType t where t.code='" + code + "'");
			if(count>0){
				ldb.set("success", true);
				ldb.set("valid", false);
				ldb.set("reason", "指标类别代码重复。");
			}else{
				ldb.set("success", true);
				ldb.set("valid", true);
				ldb.set("reason", "指标类别代码不重复，可以使用。");
			}
		}else if("u".equals(this.crud)){
			Integer count = indicationManager.getCount("select count(*) from IndicationType t where t.code='" + code + "' and t.guid !='" + this.node + "'");
			if(count>0){
				ldb.set("success", true);
				ldb.set("valid", false);
				ldb.set("reason", "指标类别代码重复。");
			}else{
				ldb.set("success", true);
				ldb.set("valid", true);
				ldb.set("reason", "指标类别代码不重复，可以使用。");
			}
		}
		
		this.json = JSONObject.fromObject(ldb).toString();
		
		return AJAX;
	}
	
	@Action(value="validateIndiCode")
	public String validateIndiCode() throws Exception {
		LazyDynaBean ldb = new  LazyDynaBean();		
		String code = this.value.trim();
		if("c".equals(this.crud)){
			Integer count = indicationManager.getCount("select count(*) from Indication t where t.code='" + code + "'");
			if(count>0){
				ldb.set("success", true);
				ldb.set("valid", false);
				ldb.set("reason", "指标代码重复。");
			}else{
				ldb.set("success", true);
				ldb.set("valid", true);
				ldb.set("reason", "指标代码不重复，可以使用。");
			}
		}else if("u".equals(this.crud)){
			Integer count = indicationManager.getCount("select count(*) from Indication t where t.code='" + code + "' and t.guid !='" + this.node + "'");
			if(count>0){
				ldb.set("success", true);
				ldb.set("valid", false);
				ldb.set("reason", "指标代码重复。");
			}else{
				ldb.set("success", true);
				ldb.set("valid", true);
				ldb.set("reason", "指标代码不重复，可以使用。");
			}
		}
		
		this.json = JSONObject.fromObject(ldb).toString();
		
		return AJAX;
	}

	@Action(value="prepareIndiEdit",
			results={@Result(name="success", location="/admin/indication/indiEdit.jsp")}
	)
	public String prepareIndiEdit()throws Exception {
		if(indication.getGuid()==null || indication.getGuid().trim().length()==0){
			indication.setGuid(new GUID().toString(32));
			indication.setTabIndex(100d);
		}else{
			indication = (Indication)indicationManager.getObject(Indication.class, indication.getGuid());
		}
		
		if(indication.getDataType()==null){
			indication.setDataType(12);
		}
		
		return SUCCESS;
	}
	
	@Action(value="updateIndi")
	public String updateIndi() throws Exception {
		JSONObject jsonObj = new JSONObject();
		try{
			if(indication.getParentIndication()!=null && indication.getParentIndication().getGuid().trim().length()==0){
				indication.setParentIndication(null);
			}
			indicationManager.saveObject(indication);
			
			jsonObj.element("success", true);
			jsonObj.element("reason", "保存成功!");
			
			jsonObj.element("guid", indication.getGuid());
			jsonObj.element("code", indication.getCode());
			jsonObj.element("shortName", indication.getShortName());
			jsonObj.element("fullName", indication.getFullName());
			
			this.json = jsonObj.toString();
		}catch(Exception e){
			jsonObj.element("success", false);
			jsonObj.element("reason", e.getMessage());
			this.json = jsonObj.toString();
		}
		
		return AJAX;
	}
	
	@Action(value="removeIndi")
	public String removeIndi() throws Exception {
		JSONObject jsonObj = new JSONObject();
		try{
			indicationManager.removeObject(Indication.class, node);
			
			jsonObj.element("success", true);
			jsonObj.element("reason", "删除成功!");
			this.json = jsonObj.toString();
		}catch(Exception e){
			jsonObj.element("success", false);
			jsonObj.element("reason", e.getMessage());
			this.json = jsonObj.toString();
		}		
		
		return AJAX;
	}

	public Indication getIndication() {
		return indication;
	}

	public void setIndication(Indication indication) {
		this.indication = indication;
	}

	public IndicationType getIndicationType() {
		return indicationType;
	}

	public void setIndicationType(IndicationType indicationType) {
		this.indicationType = indicationType;
	}

	public IndicationManager getIndicationManager() {
		return indicationManager;
	}

	public void setIndicationManager(IndicationManager indicationManager) {
		this.indicationManager = indicationManager;
	}

	public DbMetaDataManager getDbMetaDataManager() {
		return dbMetaDataManager;
	}

	public void setDbMetaDataManager(DbMetaDataManager dbMetaDataManager) {
		this.dbMetaDataManager = dbMetaDataManager;
	}

	public String getIndicationTypeGuid() {
		return indicationTypeGuid;
	}

	public void setIndicationTypeGuid(String indicationTypeGuid) {
		this.indicationTypeGuid = indicationTypeGuid;
	}

	public String getCrud() {
		return crud;
	}

	public void setCrud(String crud) {
		this.crud = crud;
	}

	public String getField() {
		return field;
	}

	public void setField(String field) {
		this.field = field;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
	
	
}